angular.module('kityminderEditor')
    .directive('exportBtn', function () {
        return {
            restrict: 'E',
            templateUrl: 'ui/directive/exportBtn/exportBtn.html',
            scope: {
                minder: '='
            },
            replace: true,
            link: function ($scope) {
                var minder = $scope.minder;

                $scope.items = ['JSON', 'TEXT', 'MD', 'SVG', 'PNG'];

                function saveText(data, filename) {
                    if (!data) {
                        alert('保存的数据为空');
                        return;
                    }
                    if (!filename)
                        return
                    if (typeof data === 'object') {
                        data = JSON.stringify(data, undefined, 4)
                    }
                    var blob = new Blob([data], { type: 'text/json' }),
                        e = document.createEvent('MouseEvents'),
                        a = document.createElement('a')
                    a.download = filename
                    a.href = window.URL.createObjectURL(blob)
                    a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
                    console.log(a);
                    e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
                    a.dispatchEvent(e)
                }
                function saveImgFromBase64(base64) {
                    const fileTypeMatch = base64.match(/^data:image\/(png|jpeg|jpg);base64,/);
                    const fileType = fileTypeMatch ? fileTypeMatch[1] : null;
                    const byteCharacters = atob(
                      base64.replace(/^data:image\/(png|jpeg|jpg);base64,/, "")
                    );
                    const byteNumbers = new Array(byteCharacters.length);
                    for (let i = 0; i < byteCharacters.length; i++) {
                      byteNumbers[i] = byteCharacters.charCodeAt(i);
                    }
                    const byteArray = new Uint8Array(byteNumbers);
                    const blob = new Blob([byteArray], {
                      type: undefined,
                    });
                    const aLink = document.createElement("a");
                    aLink.download = "图片名称." + fileType; //这里写保存时的图片名称
                    aLink.href = URL.createObjectURL(blob);
                    aLink.click();
                  }
                function saveImgFromSVG(svg){
                    const blob = new Blob([svg], { type: "image/svg+xml" });
                    const url = URL.createObjectURL(blob);
                    const a = document.createElement("a");
                    a.href = url;
                    a.download = "mindmap.svg";
                    a.click();
                    URL.revokeObjectURL(url);
                }
                $scope.export = {
                    JSON: function () {
                        minder.exportData("json").then(json => {
                            saveText(json, 'resume.json')
                        })
                    },
                    TEXT: function () {
                        minder.exportData("text").then(text => {
                            saveText(text, 'resume.txt')
                        })
                    },
                    MD: function () {
                        minder.exportData("markdown").then(md => {
                            saveText(md, 'resume.md')
                        })
                    },
                    SVG: function () {
                        minder.exportData("svg").then(svg => {
                            saveImgFromSVG(svg)
                        })
                    },
                    PNG: function () {
                        minder.exportData("png").then(base64 => {
                            saveImgFromBase64(base64)
                        })
                    }
                };
            }
        }
    });